package com.tangcco.lockwell.controller.student;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSON;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.DataDictionary;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.domain.VocabularyList;
import com.tangcco.lockwell.service.DataDictionaryService;
import com.tangcco.lockwell.service.VocabularyListService;
import com.tangcco.lockwell.viewmodel.admin.user.UserResponseVM;
import com.tangcco.lockwell.viewmodel.student.vocabularyList.VocabularyListResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生词本相关功能
 */
@RequestMapping("/api/student/vocabularyList")
@RestController("StudentVocabularyController")
public class VocabularyListController {

    @Resource
    private VocabularyListService vocabularyListService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private WebContext webContext;
    @Resource
    private RestTemplate transClient;
    /**
     * 分页查询
     * @return
     */
   @RequestMapping("page")
    public RestResponse<VocabularyListResponseVM> page(){

       return RestResponse.ok();
   }

   @RequestMapping("questionType")
    public RestResponse<List<DataDictionary>> questionType(){
       DataDictionary dataDictionary = new DataDictionary();
       dataDictionary.setCategoryCode("question_type");
       dataDictionary.setParentId(1);
       List<DataDictionary> questionType = dataDictionaryService.dataDictionarySelective(dataDictionary);
       return RestResponse.ok(questionType);
   }

    /**
     * 新增生词
     * @param vocabularyList
     * @return
     */
   @RequestMapping("/add")
    public RestResponse add(@RequestBody VocabularyList vocabularyList){
       vocabularyList.setUserId(webContext.getCurrentUser().getUserId());
       vocabularyListService.insertVocabulary(vocabularyList);
       return RestResponse.ok();
   }
    /**
     * 新增生词
     * @param vocabularyList
     * @return
     */
   @RequestMapping("/addOrDelete")
    public RestResponse addOrDelete(@RequestParam("type")Integer type,@RequestBody VocabularyList vocabularyList){
       VocabularyList request = new VocabularyList();
       request.setUserId(webContext.getCurrentUser().getUserId());
       request.setNewWord(vocabularyList.getNewWord());
       List<VocabularyList> vocabularyLists = vocabularyListService.selectByCondition(request);
       if (CollectionUtil.isNotEmpty(vocabularyLists)) {
           vocabularyLists.stream().forEach(item -> {
               vocabularyListService.deleteVocabularyById(item.getVocabularyId());
           });
       }
       if (type == 1) {
           vocabularyList.setUserId(webContext.getCurrentUser().getUserId());
           //新增生词
           vocabularyListService.insertSelective(vocabularyList);
       }
       return RestResponse.ok();
   }

    /**
     * 编辑生词
     * @param vocabularyList
     * @return
     */
   @RequestMapping("/update")
    public RestResponse update(@RequestBody VocabularyList vocabularyList){
       vocabularyListService.updateByPrimaryKeySelective(vocabularyList);
       return RestResponse.ok();
   }

    @RequestMapping("/select")
    public RestResponse<List<VocabularyListResponseVM>> select() {
        List<VocabularyListResponseVM> vocabularyListResponseVMS = vocabularyListService.selectVocabularyByUserId(webContext.getCurrentUser().getUserId());
        return RestResponse.ok(vocabularyListResponseVMS);
    }
}
